package com.wanmait.shoppingsys.controller.manage;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wanmait.shoppingsys.dao.AdminDAO;
import com.wanmait.shoppingsys.util.ManagePager;
import com.wanmait.shoppingsys.vo.Admin;


@WebServlet("/AdminController")
public class AdminController extends HttpServlet{
	private AdminDAO adminDAO=AdminDAO.getDAO();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		 request.setCharacterEncoding("UTF-8");
		 String action=request.getParameter("action");
		 if(action==null)
		 {
			 return;
		 }
		 switch(action) 
		 {
		 case "admin":   //管理员列表
	     this.admin(request, response);
		     break;
		 case "delete":   //物理删除
			 this.delete(request, response);
			 break;
		 case "delete1":   //物理删除
			 this.delete1(request, response);
			 break;
		 case "eidt":
			 this.eidt(request, response);
			 break;
		 case "save":
			 this.save(request, response);
			 break;
		 case "reg":
		 this.reg(request, response);
		    break;
		 }	    
	}
	
	
	
	protected void reg(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String path=request.getServletContext().getContextPath();
		String name = request.getParameter("name");
		String pass = request.getParameter("pass");
		String realname = request.getParameter("realname");
		String actor = request.getParameter("actor");
		
		
		ArrayList<Admin> admins = (ArrayList<Admin>)adminDAO.findAll();
		int k=0;
		for(int i=0;i<admins.size();i++) {
			Admin admin = admins.get(i);
			if(name.equals(admin.getName()))
			{
				response.sendRedirect(path+"/administrator/register.jsp?err=1");
			}
			else {
				k++;
			}
		}
		if(admins.size()==k) {
			Admin admin=new Admin();
			admin.setName(name);
			admin.setPass(pass);
			admin.setRealName(realname);
			admin.setActor(actor);
			adminDAO.insert(admin);
			response.sendRedirect("/ShoppingSys/AdminController?action=admin");
		}
		
	
	}
	
	protected void save(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		String idStr = request.getParameter("id");
		String name = request.getParameter("name");
		String realname = request.getParameter("realname");
		String pass = request.getParameter("pass");
		Integer id = Integer.parseInt(idStr);
		
		Admin admin=new Admin();
		admin.setId(id);
	    admin.setName(name);
	    admin.setRealName(realname);
	    admin.setPass(pass);
	    
		adminDAO.update(admin);
		response.sendRedirect("/ShoppingSys/AdminController?action=admin");
	}
	
	protected void eidt(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String idStr = request.getParameter("id");
		Integer  id = Integer.parseInt(idStr);
		Admin admin = adminDAO.findById(id);
		request.setAttribute("admin", admin);
		request.getRequestDispatcher("/administrator/eidt.jsp").forward(request, response);
	}
	
	
	protected void admin(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<Admin> admins=adminDAO.findAll();
		request.setAttribute("admins", admins);
		request.getRequestDispatcher("/administrator/list.jsp").forward(request, response);
	}
	protected void delete(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		String IdStr = request.getParameter("Id");
		Integer  Id = Integer.parseInt(IdStr);
		adminDAO.delete(Id);
		this.admin(request, response);
		
	}
	
	protected void delete1(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
		String idStr = request.getParameter("id");
		 Integer id = Integer.parseInt(idStr);
		 String enStr = request.getParameter("en");
		 Integer en = Integer.parseInt(enStr);
		adminDAO.update1(id,en);
		
	}
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
}
